﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using public_dbOpt;

namespace prjEMR22_154
{
    public partial class Inspect_exdit : Form
    {
        dbOPT dataOpt = new dbOPT();
        public static string strInspectID;

        public Inspect_exdit()
        {
            InitializeComponent();
        }

        private void Inspect_exdit_Load(object sender, EventArgs e)
        {
            //绑定科室下拉列表
            dataOpt.BindDropdownlist("departinfo", DepartName, 1);

            if (Inspect.New_check)
            {// 假设你有一个MenuStrip控件名为menuStrip1  
                // 并且你想要删除具有特定Text属性的多个菜单项  
                // 创建一个要删除的菜单项Text的列表  
                List<string> menuItemsToRemove = new List<string> { "编辑", "取消", "删除项目" };

                List<ToolStripMenuItem> itemsToRemove = new List<ToolStripMenuItem>();
                foreach (ToolStripMenuItem item in menuStrip1.Items)
                {
                    if (menuItemsToRemove.Contains(item.Text))
                    {
                        itemsToRemove.Add(item);
                    }
                    // 注意：这里不应该调用递归方法，因为我们还没有处理完所有父项。  
                }

                // 现在删除收集到的项  
                foreach (ToolStripMenuItem item in itemsToRemove)
                {
                    menuStrip1.Items.Remove(item);
                    // 注意：如果item有子项，并且你也想删除它们，  
                    // 你需要在这里添加额外的逻辑来处理子项的删除。  
                }

                OpenBoxes(this);
                label18.Visible = false;
            }

            else
            {
                strInspectID = Inspect.strInspectID;
                string sql = String.Format("select * from inspectinfo where InspectID = '{0}';", strInspectID);
                DataSet ds = new DataSet();//创建数据集
                ds = dataOpt.GetTable(sql);
                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    // 访问数据集中的数据  
                    DataRow row = ds.Tables[0].Rows[0]; // 获取第一行数据  
                    InitBoxes(this, row);//初始化设置各个控件的初始值
                }
                CloseBoxes(this);
            }
        }

        private void InitBoxes(Control parent, DataRow row)  //初始化设置各个控件的初始值
        {
            foreach (Control ctrl in parent.Controls)
            {
                if (ctrl is TextBox)
                {
                    TextBox textBox = (TextBox)ctrl;
                    string columnName = ctrl.Name;

                    // 检查列是否存在并尝试获取值  
                    if (row.Table.Columns.Contains(columnName))
                    {
                        object value = row[columnName];
                        if (value != DBNull.Value) // 检查值是否为 DBNull  
                        {
                            textBox.Text = value.ToString();
                        }
                    }
                    // 如果列不存在，可以选择记录日志、抛出异常或执行其他操作  
                    // else { /* 处理列不存在的情况 */ }  
                }

                if (ctrl is ComboBox)
                {
                    ComboBox textBox = (ComboBox)ctrl;

                    if (row.Table.Columns.Contains(ctrl.Name))
                    {
                        object value = row[ctrl.Name];
                        if (value != DBNull.Value) // 检查值是否为 DBNull  
                        {
                            textBox.Text = value.ToString();
                        }
                    }
                    // 如果列不存在，可以选择记录日志、抛出异常或执行其他操作  
                    // else { /* 处理列不存在的情况 */ } 
                }
                // 递归遍历子控件  
                //if (ctrl.Controls.Count > 0)
                //{
                //    InitBoxes(ctrl, row);
                //}
            }

        }

        private void CloseBoxes(Control parent)  //关闭编辑模式函数
        {
            foreach (Control ctrl in parent.Controls)
            {
                if (ctrl is TextBox)
                {
                    TextBox textBox = (TextBox)ctrl;
                    textBox.ReadOnly = true;
                }

                if (ctrl is ComboBox)
                {
                    ComboBox textBox = (ComboBox)ctrl;
                    textBox.Enabled = false;
                }
                // 递归遍历子控件  
                if (ctrl.Controls.Count > 0)
                {
                    CloseBoxes(ctrl);
                }
            }

        }

        private void OpenBoxes(Control parent)  //打开编辑模式函数
        {
            foreach (Control ctrl in parent.Controls)
            {
                if (ctrl is TextBox)
                {
                    TextBox textBox = (TextBox)ctrl;
                    textBox.ReadOnly = false;
                }

                if (ctrl is ComboBox)
                {
                    ComboBox textBox = (ComboBox)ctrl;
                    textBox.Enabled = true;
                }
                // 递归遍历子控件  
                //if (ctrl.Controls.Count > 0)
                //{
                //    OpenBoxes(ctrl);
                //}
            }

        }

        private void OpenExdit_Click(object sender, EventArgs e)
        {
            OpenBoxes(this);
            label18.Text = "编辑模式";
        }

        private void CencelExdit_Click(object sender, EventArgs e)
        {
            CloseBoxes(this);
            label18.Text = "只读模式";
        }

        private void Delete_Click(object sender, EventArgs e)
        {
            MR_contrl_exditDE.Word = "Inspect_exdit";
            MR_contrl_exditDE a = new MR_contrl_exditDE();  //展开病人数据
            a.ShowDialog(this);
            if (MR_contrl_exditDE.inspect_check) //已删除，清除文本
            {
                foreach (Control control in this.Controls)
                {
                    if (control is TextBox)
                    {
                        ((TextBox)control).Text = "";
                    }
                    if (control is ComboBox)
                    {
                        ((ComboBox)control).Text = "";
                    }

                }
            }
        }

        private void Cencel_Click(object sender, EventArgs e)
        {
            string sql = String.Format("select * from inspectinfo where InspectID = '{0}';", strInspectID);
            DataSet ds = new DataSet();//创建数据集
            ds = dataOpt.GetTable(sql);

            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                // 访问数据集中的数据  
                DataRow row = ds.Tables[0].Rows[0]; // 获取第一行数据  
                InitBoxes(this, row);//初始化设置各个控件的初始值
            }
        }

        private void Save_Click(object sender, EventArgs e)
        {
            if (InspectID.Text != "" && DepartName.Text != "" && 检查项目.Text != "")
            {
                if (Inspect.New_check)
                {
                    new_check();
                }
                else
                {
                    foreach (Control ctrl in this.Controls)
                    {
                        if (ctrl is TextBox)
                        {
                            TextBox textbox = (TextBox)ctrl;
                            string text_name = textbox.Name == "Pa_Name" ? "Name" : textbox.Name;
                            string sql = String.Format("UPDATE inspectinfo SET {0} = '{1}' where InspectID = '{2}'", text_name, textbox.Text, strInspectID);   //set后跟的属性不能加''
                            dataOpt.OperateData(sql);
                        }

                        if (ctrl is ComboBox)
                        {
                            ComboBox textbox = (ComboBox)ctrl;
                            string sql = String.Format("UPDATE inspectinfo SET {0} = '{1}' where InspectID = '{2}'", textbox.Name, textbox.Text, strInspectID);
                            dataOpt.OperateData(sql);
                        }
                    }
                    MessageBox.Show("保存成功");
                    strInspectID = InspectID.Text;
                }
            }
            else
            {
                MessageBox.Show("必填项有空项，请重新输入");
            }
        }

        private void new_check()
        {
            string sql = String.Format("select * from inspectinfo where InspectID ='{0}'", InspectID.Text);
            DataSet ds = new DataSet();//创建数据集
            ds = dataOpt.GetTable(sql);

            if (ds != null && ds.Tables[0].Rows.Count != 0)
            {
                // 处理用户存在的情况  
                MessageBox.Show("项目编号已存在。");
            }
            else
            {
                sql = String.Format("INSERT INTO inspectinfo (InspectID) VALUES ('{0}')", InspectID.Text);
                dataOpt.OperateData(sql);
                foreach (Control ctrl in this.Controls)
                {
                    if (ctrl is TextBox)
                    {
                        TextBox textbox = (TextBox)ctrl;
                        string text_name = textbox.Name;
                        sql = String.Format("UPDATE inspectinfo SET {0} = '{1}' where InspectID = '{2}'", text_name, textbox.Text, InspectID.Text);   //set后跟的属性不能加''
                        dataOpt.OperateData(sql);
                    }

                    if (ctrl is ComboBox)
                    {
                        ComboBox textbox = (ComboBox)ctrl;
                        sql = String.Format("UPDATE inspectinfo SET {0} = '{1}' where InspectID = '{2}'", textbox.Name, textbox.Text, InspectID.Text);
                        dataOpt.OperateData(sql);
                    }
                }
                MessageBox.Show("保存成功");
                strInspectID = InspectID.Text;
            }
        }


    }
}
